﻿#region | Copyright © 2556 |

/*
##########################################################################
	Author By : Siriwat Suananpornpanit
	DateTime : 2556/08/30
##########################################################################
*/

#endregion

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using DataAccess;
using Web.Business;

namespace Web.Core
{
    public partial class SessionPool
    {
        private static SessionPool _sessionPool;

        public static SessionPool GetInstance()
        {
            _sessionPool = new SessionPool();
            return _sessionPool;
        }

        internal void SetSession(System.Web.UI.Page page, string username, string companyCode = "HP00")
        {
            p_USER_PROFILE list = UserProfileBal.GetInstance().Select(page, new p_USER_PROFILE()
                                                                        {
                                                                            EMPNO = username,
                                                                            COMP_CODE = companyCode
                                                                        }
                                                                    ).First();
            if (list!= null)
                {
                    System.Web.HttpContext.Current.Session.Remove("UserInfo");
                    System.Web.HttpContext.Current.Session["UserInfo"] = list;
                }
        }

        internal p_USER_PROFILE GetSession(System.Web.UI.Page page)
        {
            try
            {
                   p_USER_PROFILE user = (p_USER_PROFILE)System.Web.HttpContext.Current.Session["UserInfo"];
                   return user;
            }
            catch (System.Exception ex)
            {
                //
                Core.Modal.Exception(
                   page: page,
                   message: Core.Exception.ModalException(
                                       method: System.Reflection.MethodBase.GetCurrentMethod(),
                                       ex: ex));
                //
                System.Web.HttpContext.Current.Response.Redirect("~/index.aspx");
                return null;
            }
            
        }

        internal void CheckSession()
        {
            if (System.Web.HttpContext.Current.Session["UserInfo"] == null)
            {
                System.Web.HttpContext.Current.Response.Redirect("~/index.aspx");
            }
        }

    }
}